$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'erp/customerprice/list',
        datatype: "json",
        colModel: [	
        	{ label: 'customerPriceId', name: 'customerPriceId', index: 'customer_price_id', width: 50, key: true,hidden:true },
			{ label: '客户', name: 'customerName', index: 'customer_name', width: 80 }, 			
			{ label: '纸质', name: 'paperCode', index: 'paper_info_id', width: 80 }, 			
			{ label: '纸类', name: 'paperType', index: 'paper_type', width: 80 }, 			
			{ label: '纸质编码', name: 'paperCode', index: 'paper_code', width: 80 }, 			
			{ label: '调整纸质', name: 'changePaperCode', index: 'change_paper_id', width: 80 }, 			
			{ label: '最新报价', name: 'newPrice', index: 'new_price', width: 80 }, 			
			{ label: '上次报价', name: 'oldPrice', index: 'old_price', width: 80 }, 			
			{ label: '供应商编码', name: 'supplierCode', index: 'supplier_code', width: 80 }, 			
			{ label: '供应商', name: 'supplierName', index: 'supplier_info_id', width: 80 }	
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		customerPrice: {},
		customerVO: {},
		customerList: [],
		customerCode: '',
		paperCodeList: [],
		paperCode: '',
		paperInfoId: '',
		customerName: '',
		paperCodeVO: {},
		changePaperCodeVO: {},
		changePaperCode: '',
		supplierCode: '',
		supplierCodeVO: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.customerPrice = {};
		},
		update: function (event) {
			var customerPriceId = getSelectedRow();
			if(customerPriceId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(customerPriceId)
		},
		saveOrUpdate: function (event) {
			debugger;
			vm.customerPrice.paperInfoId=vm.paperInfoId;
			vm.customerPrice.supplierCode=vm.supplierCode;
			vm.customerPrice.status=1;
			vm.customerPrice.changePaperCode=vm.changePaperCode;
			vm.customerPrice.customerName=vm.customerName;
			var url = vm.customerPrice.customerPriceId == null ? "erp/customerprice/save" : "erp/customerprice/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.customerPrice),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var customerPriceIds = getSelectedRows();
			if(customerPriceIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "erp/customerprice/delete",
                    contentType: "application/json",
				    data: JSON.stringify(customerPriceIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(customerPriceId){
			$.get(baseURL + "erp/customerprice/info/"+customerPriceId, function(r){
                vm.customerPrice = r.customerPrice;
                var customerInfoId = r.customerPrice.customerInfoId;
                var supplierInfoId = r.customerPrice.supplierInfoId;
                vm.paperCode = r.customerPrice.paperCode;
                vm.changePaperCode = r.customerPrice.changePaperCode;
                for(var i=0;i<vm.customerList.length;i++){
                	if(customerInfoId==vm.customerList[i].customerInfoId){
                		vm.customerVO=vm.customerList[i];
                	}
                	if(supplierInfoId==vm.customerList[i].customerInfoId){
                		vm.supplierCodeVO=vm.customerList[i];
                	}
                	if(vm.customerVO.customerCode && vm.supplierCodeVO.customerCode){
                		break;
                	}
                }
                debugger;
                vm.customerCode=vm.customerVO.customerCode;
                vm.supplierCode=vm.supplierCodeVO.customerCode;
                vm.customerName=r.customerPrice.customerName;
                vm.paperInfoId=r.customerPrice.paperInfoId;
                
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},
		changeCustomer:function(type){
			if(type===2){
				for(var i=0;i<this.customerList.length;i++){
					if(this.supplierCode==this.customerList[i].customerCode){
						this.supplierCodeVO=this.customerList[i];
						this.customerPrice.supplierInfoId=this.supplierCodeVO.customerInfoId;
						break;
					}
				}
			}else{
				for(var i=0;i<this.customerList.length;i++){
					if(this.customerCode==this.customerList[i].customerCode){
						this.customerVO=this.customerList[i];
						this.customerPrice.customerInfoId=this.customerVO.customerInfoId;
						this.customerName=this.customerVO.customerName;
						break;
					}
				}
			}
			
		},
		getCustomerList: function(){
			$.get(baseURL + "erp/customerinfo/list", 
				{
			        page:{"currPage":1,"limit":10}
			    },
			    function(r){
			        if(r.code==0){
			        	vm.customerList=r.page.list;
			        }
			    }
			);
		},
		queryPaperInfo() {
			$.get(baseURL + "erp/paperinfo/list", 
			{
	            page:{"currPage":1,"limit":10}
		        },
		        function(r){
	                if(r.code==0){
	                	vm.paperCodeList=r.page.list;
	                }
	            }
	        );
			
	   },
	   changePaper:function(type){
		   debugger;
		   if(type===2){
			   for(var i=0;i<this.paperCodeList.length;i++){
					if(this.changePaperCode==this.paperCodeList[i].paperCode){
						this.changePaperCodeVO=this.paperCodeList[i];
						this.customerPrice.changePaperId=this.changePaperCodeVO.paperInfoId;
						this.changePaperCode=this.changePaperCodeVO.paperCode;
						break;
					}
				}
		   }else{
			   for(var i=0;i<this.paperCodeList.length;i++){
					if(this.paperCode==this.paperCodeList[i].paperCode){
						this.paperCodeVO=this.paperCodeList[i];
						this.customerPrice.paperType=this.paperCodeVO.paperType;
						this.customerPrice.paperCode=this.paperCodeVO.paperCode;
						this.paperInfoId=this.paperCodeVO.paperInfoId;
						break;
					}
				}
		   }
			
		},
	},
	mounted() {
	      this.getCustomerList();
	      this.queryPaperInfo();
	}
});